package com.atguigu.day03;

import java.util.Currency;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

//循环栅栏   CyclicBarrier    就是一直往上加，直到某一个数值之后才会执行main方法内的线程
//集齐7颗龙珠，召唤神龙
//相当于人到齐了才能开会
public class CyclicBarrierDemo {

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙！！！");
        });
        for (int i = 1; i <=7 ; i++) {
            int finalI = i;
            new Thread(() -> {
                 System.out.println("线程"+Thread.currentThread().getName()+"集齐第"+ finalI +"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }, String.valueOf(i)).start();
        }




    }
}
